<template>
  <div class="treeItem">
    <span class="title">{{ item.name }}</span>
    <span>{{ item.manager ? item.manager : '负责人' }}</span>

    <el-dropdown @command="handleCommand">
      <span>操作<i class="el-icon-arrow-down el-icon--right" /></span>
      <el-dropdown-menu>
        <el-dropdown-item command="add"> 添加子部门 </el-dropdown-item>
        <el-dropdown-item v-if="item.manager" command="look">
          查看部门</el-dropdown-item>
        <el-dropdown-item v-if="item.manager" command="del">
          删除部门
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</template>
<script>
import { companyDepartmentDelete } from '@/api/departments'
export default {
  name: 'TreeItem',
  props: {
    item: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {}
  },
  methods: {
    handleCommand(command) {
      switch (command) {
        case 'add':
          this.$bus.$emit('isShow', true, this.item)
          break
        case 'look':
          this.$bus.$emit('isShow', true, this.item, 'edit')
          break
        case 'del':
          this.$confirm(
            `是否确认删除 '${this.item.name}' 部门`,
            '温馨提示'
          ).then(async() => {
            await companyDepartmentDelete(this.item.id)
            this.$message.success('删除成功')
            this.$emit('getdata')
          })
          break
        default:
          break
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.treeItem {
  display: flex;
  width: 100%;
  align-items: center;

  .title {
    flex: 1;
  }
  span {
    padding: 0 10px;
  }
}
</style>
